package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.PopulationFlow;
import com.example.poverty.service.PopulationFlowService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/population")
public class PopulationFlowController {

    @Autowired
    private PopulationFlowService populationFlowService;

    /**
     * 添加人口流动人员
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addPopulationFlow(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name").trim();  // 主键
        String sex = request.getParameter("sex").trim();  //性别
        String nationality = request.getParameter("nationality").trim();  //民族
        String education = request.getParameter("education").trim();  //文化程度
        String identificationNumber = request.getParameter("identificationNumber").trim();  //身份证号码
        String accountTypes = request.getParameter("accountTypes").trim();  //户口类型
        String localPoliceStation = request.getParameter("localPoliceStation").trim();  //户口所在地派出所
        String affiliatedVillage = request.getParameter("affiliatedVillage").trim();  //所属村镇
        String residency = request.getParameter("residency").trim();  //居住事由
        String residentialAddress = request.getParameter("residentialAddress").trim();  //居住地址
        String currentlyEngagedOccupation = request.getParameter("currentlyEngagedOccupation").trim();  //现从事职业
        String unitAddress = request.getParameter("unitAddress").trim();  //单位地址

        PopulationFlow populationFlow = new PopulationFlow();
        populationFlow.setName(name);
        populationFlow.setSex(new Byte(sex));
        populationFlow.setNationality(nationality);
        populationFlow.setEducation(new Byte(education));
        populationFlow.setIdentificationNumber(identificationNumber);
        populationFlow.setAccountTypes(new Byte(accountTypes));
        populationFlow.setLocalPoliceStation(localPoliceStation);
        populationFlow.setAffiliatedVillage(affiliatedVillage);
        populationFlow.setResidency(residency);
        populationFlow.setResidentialAddress(residentialAddress);
        populationFlow.setCurrentlyEngagedOccupation(currentlyEngagedOccupation);
        populationFlow.setUnitAddress(unitAddress);
        boolean flag = populationFlowService.insert(populationFlow);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改流动人口信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updatePopulationFlow(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String name = request.getParameter("name").trim();  // 主键
        String sex = request.getParameter("sex").trim();  //性别
        String nationality = request.getParameter("nationality").trim();  //民族
        String education = request.getParameter("education").trim();  //文化程度
        String identificationNumber = request.getParameter("identificationNumber").trim();  //身份证号码
        String accountTypes = request.getParameter("accountTypes").trim();  //户口类型
        String localPoliceStation = request.getParameter("localPoliceStation").trim();  //户口所在地派出所
        String affiliatedVillage = request.getParameter("affiliatedVillage").trim();  //所属村镇
        String residency = request.getParameter("residency").trim();  //居住事由
        String residentialAddress = request.getParameter("residentialAddress").trim();  //居住地址
        String currentlyEngagedOccupation = request.getParameter("currentlyEngagedOccupation").trim();  //现从事职业
        String unitAddress = request.getParameter("unitAddress").trim();  //单位地址

        PopulationFlow populationFlow = new PopulationFlow();
        populationFlow.setId(Integer.parseInt(id));
        populationFlow.setName(name);
        populationFlow.setSex(new Byte(sex));
        populationFlow.setNationality(nationality);
        populationFlow.setEducation(new Byte(education));
        populationFlow.setIdentificationNumber(identificationNumber);
        populationFlow.setAccountTypes(new Byte(accountTypes));
        populationFlow.setLocalPoliceStation(localPoliceStation);
        populationFlow.setAffiliatedVillage(affiliatedVillage);
        populationFlow.setResidency(residency);
        populationFlow.setResidentialAddress(residentialAddress);
        populationFlow.setCurrentlyEngagedOccupation(currentlyEngagedOccupation);
        populationFlow.setUnitAddress(unitAddress);
        boolean flag = populationFlowService.update(populationFlow);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除流动人口信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deletePopulationFlow(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = populationFlowService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询流动人口信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return populationFlowService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的流动人口信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allPopulationFlow", method = RequestMethod.GET)
    public Object allPopulationFlow(HttpServletRequest request) {
        return populationFlowService.allPopulationFlow();
    }

    /**
     * 根据姓名查询流动人口信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();  // 主键
        return populationFlowService.getByName(name);
    }
}
